﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using BluePortal.Medias.Core;
using System.IO;

namespace BluePortal.QuikStart
{
    public partial class UploadPhoto : System.Web.UI.Page
    {
        public Guid id;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                id = Guid.NewGuid();
                AddTagVideo1.Visible = false;
                AddCategory1.Visible = false;
            }
            else
            {
                BCPicture bc = new BCPicture();
                Picture p = new Picture();
                p.ID = id;
                p.Title = tboxTitle.Text;
                p.Description = tboxDescription.Text;
                p.PublishDate = DateTime.Now;
                p.Image = fileupload1.FileBytes;
                using (MemoryStream image = new MemoryStream(fileupload1.FileBytes))
                {
                    System.Drawing.Image bitmap = new System.Drawing.Bitmap(image);
                    System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                    System.Drawing.Image thumbnailImage = bitmap.GetThumbnailImage(160, (160 * bitmap.Height) / bitmap.Width, myCallback, IntPtr.Zero);
                    using (MemoryStream preview = new MemoryStream())
                    {
                        thumbnailImage.Save(preview, System.Drawing.Imaging.ImageFormat.Jpeg);
                        p.Preview = preview.ToArray();
                    }
                }
                bc.Create(p);
                AddTagVideo1.Visible = true;
                AddCategory1.Visible = true;

            }
        }

        public bool ThumbnailCallback()
        {
            return false;
        }
    }
}
